using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Net;
using CustomListView;

namespace HistoryOfTrading
{
    public struct Company
    {
        public string code;
        public string name;
    }

    public struct Session
    {
        public float max;
        public float min;
        public float close;
        public int volume;
        public DateTime date;
    }

	/// <summary>
	/// Summary description for ctlMain.
	/// </summary>
	public class ctlMain : System.Windows.Forms.UserControl
    {
        private string currency;
        private ListViewEx lvHistoryOfTrading;
        private ColumnHeader chDate;
        private ColumnHeader chCode;
        private ColumnHeader chReason;
        private ColumnHeader chBuyPrice;
        private ColumnHeader chVol;
        private ColumnHeader chRewardVol;
        private ColumnHeader chSPO;
        private ColumnHeader chDividend;
        private ColumnHeader chCurPrice;
        private ColumnHeader chSoldPrice;
        private ColumnHeader chInterestOrLoss;
        private ColumnHeader chInterestOrLossPerDay;
        private ColumnHeader chLesson;
        private Button btnProcess;
        private string sHistoryFilePath;

		public ctlMain()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
                SaveHistoryTrading();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.lvHistoryOfTrading = new CustomListView.ListViewEx();
            this.chDate = new System.Windows.Forms.ColumnHeader();
            this.chCode = new System.Windows.Forms.ColumnHeader();
            this.chReason = new System.Windows.Forms.ColumnHeader();
            this.chBuyPrice = new System.Windows.Forms.ColumnHeader();
            this.chVol = new System.Windows.Forms.ColumnHeader();
            this.chRewardVol = new System.Windows.Forms.ColumnHeader();
            this.chSPO = new System.Windows.Forms.ColumnHeader();
            this.chDividend = new System.Windows.Forms.ColumnHeader();
            this.chCurPrice = new System.Windows.Forms.ColumnHeader();
            this.chSoldPrice = new System.Windows.Forms.ColumnHeader();
            this.chInterestOrLoss = new System.Windows.Forms.ColumnHeader();
            this.chInterestOrLossPerDay = new System.Windows.Forms.ColumnHeader();
            this.chLesson = new System.Windows.Forms.ColumnHeader();
            this.btnProcess = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lvHistoryOfTrading
            // 
            this.lvHistoryOfTrading.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lvHistoryOfTrading.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.chDate,
            this.chCode,
            this.chReason,
            this.chBuyPrice,
            this.chVol,
            this.chRewardVol,
            this.chSPO,
            this.chDividend,
            this.chCurPrice,
            this.chSoldPrice,
            this.chInterestOrLoss,
            this.chInterestOrLossPerDay,
            this.chLesson});
            this.lvHistoryOfTrading.FullRowSelect = true;
            this.lvHistoryOfTrading.GridLines = true;
            this.lvHistoryOfTrading.Location = new System.Drawing.Point(3, 34);
            this.lvHistoryOfTrading.Name = "lvHistoryOfTrading";
            this.lvHistoryOfTrading.Size = new System.Drawing.Size(989, 370);
            this.lvHistoryOfTrading.TabIndex = 0;
            this.lvHistoryOfTrading.UseCompatibleStateImageBehavior = false;
            this.lvHistoryOfTrading.View = System.Windows.Forms.View.Details;
            // 
            // chDate
            // 
            this.chDate.Text = "Date";
            this.chDate.Width = 80;
            // 
            // chCode
            // 
            this.chCode.Text = "Code";
            this.chCode.Width = 40;
            // 
            // chReason
            // 
            this.chReason.Text = "Reason";
            this.chReason.Width = 240;
            // 
            // chBuyPrice
            // 
            this.chBuyPrice.Text = "Price";
            this.chBuyPrice.Width = 40;
            // 
            // chVol
            // 
            this.chVol.Text = "Vol";
            this.chVol.Width = 40;
            // 
            // chRewardVol
            // 
            this.chRewardVol.Text = "Reward Vol";
            // 
            // chSPO
            // 
            this.chSPO.Text = "SPO Vol";
            this.chSPO.Width = 40;
            // 
            // chDividend
            // 
            this.chDividend.Text = "Dividend";
            this.chDividend.Width = 40;
            // 
            // chCurPrice
            // 
            this.chCurPrice.Text = "Cur. Price";
            this.chCurPrice.Width = 40;
            // 
            // chSoldPrice
            // 
            this.chSoldPrice.Text = "Sold. Price";
            this.chSoldPrice.Width = 40;
            // 
            // chInterestOrLoss
            // 
            this.chInterestOrLoss.Text = "Interest/Loss";
            this.chInterestOrLoss.Width = 50;
            // 
            // chInterestOrLossPerDay
            // 
            this.chInterestOrLossPerDay.Text = "InterestOrLoss/Day";
            this.chInterestOrLossPerDay.Width = 50;
            // 
            // chLesson
            // 
            this.chLesson.Text = "Lesson";
            this.chLesson.Width = 240;
            // 
            // btnProcess
            // 
            this.btnProcess.Location = new System.Drawing.Point(3, 5);
            this.btnProcess.Name = "btnProcess";
            this.btnProcess.Size = new System.Drawing.Size(75, 23);
            this.btnProcess.TabIndex = 1;
            this.btnProcess.Text = "Up and Calc";
            this.btnProcess.UseVisualStyleBackColor = true;
            this.btnProcess.Click += new System.EventHandler(this.btnProcess_Click);
            // 
            // ctlMain
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.btnProcess);
            this.Controls.Add(this.lvHistoryOfTrading);
            this.Name = "ctlMain";
            this.Size = new System.Drawing.Size(995, 421);
            this.Load += new System.EventHandler(this.ctlMain_Load);
            this.ResumeLayout(false);

		}
		#endregion

		private void ctlMain_Load(object sender, System.EventArgs e)
		{
           
            this.lvHistoryOfTrading.AddSubItem = true;
            this.lvHistoryOfTrading.AddDateTimePickerCell(-1, 0);
            for (int i = 1; i < lvHistoryOfTrading.Columns.Count; i++)
                this.lvHistoryOfTrading.AddEditableCell(-1, i);
            ComboBox cbMarkets = new ComboBox();
            // Initialize config
            StreamReader sr = new StreamReader("config.txt");
            while (!sr.EndOfStream)
            {
                switch (sr.ReadLine().Normalize())
                {
                    case "[currency]":
                        currency = sr.ReadLine().Trim();
                        break;
                    case "[History Trading]":
                        sHistoryFilePath = sr.ReadLine().Trim();
                        break;
                }
            }
            sr.Close();
            LoadHistoryTrading();
		}

        private string convertCurrency(string c)
        {
            if (currency == "VN")
                return c.Replace(",","").Replace('.', ',');
            return c.Replace(",", "");
        }

        private void SaveHistoryTrading()
        {
            StreamWriter sw = new StreamWriter(sHistoryFilePath);
            int nItems = lvHistoryOfTrading.Items.Count;
            sw.WriteLine(nItems.ToString());

            for (int i = 0; i < nItems; i++)
            {
                int nCol = lvHistoryOfTrading.Columns.Count;
                string itemString = "";
                for (int j = 0; j < nCol-1; j++)
                    itemString += lvHistoryOfTrading.Items[i].SubItems[j].Text + "\t";
                itemString += lvHistoryOfTrading.Items[i].SubItems[nCol - 1].Text;
                sw.WriteLine(itemString);
            }
            sw.Close();
        }

        private void LoadHistoryTrading()
        {
            if (!File.Exists(sHistoryFilePath))
            {
                lvHistoryOfTrading.Items.Add("");
                return;
            }
            StreamReader sr = new StreamReader(sHistoryFilePath);
            int nItem = int.Parse(sr.ReadLine());

            for (int i = 0; i < nItem; i++)
            {
                string str = sr.ReadLine();
                string[] tokens = str.Split('\t');
                int nCol = lvHistoryOfTrading.Columns.Count;

                lvHistoryOfTrading.Items.Add(tokens[0]);

                for (int j = 1; j < nCol; j++)
                    lvHistoryOfTrading.Items[i].SubItems.Add(tokens[j]);
            }
            sr.Close();
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            int nItem = lvHistoryOfTrading.Items.Count;
            for (int i = 0; i < nItem; i++)
            {
                // Lay ma cong ty
                string code = lvHistoryOfTrading.Items[i].SubItems["Code"].Text;
                if (code != "")
                {
                    // Cap nhat Cur. Price

                   // Tinh toan loi/lo

                }
            }
        }
    }
}
